'Embedded programming'

03.25.2020

Reading a datasheet

I decided to read more about the microcontroller that my board has: ATmega328 microcontroller with Optiboot (UNO) Bootloader - Datasheet. But before reading the datasheet I wanted to understand what a Optiboot Bootloader means. So I searched for it and found that the bootloader is a section of the program memory that runs before the main code (or first intruction) runs. It is used to setup a microcontroller or to allow it to update the main program's code.

In the case of the ATmega328p. This microcontroller has specifically a portion of its memory for bootlading code. When on power, this microcontroller waits a short time while paying attention to the pins. In case it doesn't receive any information, then the processor loads the "user program" section to load whatever is already in the memory, for example a sketch.



This is an 8-bit microcontroller that has 32 registers for which 23 are programmable I/O lines and a memory size of 32KBytes of Flash, 1KBytes of EEPROM (Electrically Erasable Programmable Read Only Memory) which according to what I understood this refers back to the storing of data in memory, so if the electric supply is removed the microcontroller can store the data and then provide results when reconnecting it to another electric supply. And 2KBytes of Ram.

It operates with a 3.3V to 5.5V supply, although we use 5V normally. All the I/O ports have "pull-up resistors with a supply-voltage invariant resistance" for newbies like me not to burn it. Something else that I found interesting is that the ports are bi-directional. From what I understood, it means that the direction of a port can be changed through the use of SBI / CBI instructions. Which refer to assembler instructions that work specifically with registers.



Arduino & setting IDE

Even though we've used Arduino in class before, it has been a long time since I don't use it. At the beginning I had a hard time uploading the file to my board. I knew it wasn't something related to the code as I opened and exercise file. Then I realized it was because the last time I used the software I changed the board. I own a SparkFun RedBoard, and to find which board I needed for the IDE. I went to the the webpage and found it, I can use "Arduino UNO" from the board menu (Tools/Board:Arduino Uno) to upload code.

For the first example - "Analog write". I didn't feel like taking long. So I just plugged everything together, upload the file and worked fine. Checking the Led was connected to pin 9 as instructed in the example file, and connecting it to the 5V power.



For the second example - "Digital write". I wanted to add more leds and put a delay between them, so it could be more of a rainbow (Why a rainbow? To light up our lives during COVID19 quarantine. Jk, I don't have many LEDS at home). I found a few examples on the internet and followed this tutorial. First I needed to connect the leds to a pin so I used the second pin to the twelfth. After connecting them and making sure their catode tip was connected to my ground. I uploaded the file. Inside it, I defined two for functions. One to light up the leds from left to right and other for the opposite, so the leds would make a "wave".



Finally for the third example, I wanted to "Blink without delay" but not only for one led. Maybe adjust two with different times. The led connected to number 13, worked like that (w/o delay). I tried to change the code to see if it was possible to set another interval for the led connected to number 8. I tried a few times but couldn't make it. So in the end, I used delay for the 2nd led. Setting it to every 1000 milliseconds (half the time the "Blink without delay led" was supposed to turn on). I have to say that I didn't spend a long time looking for the right way, the presentation of Design Dialogues was taking me a long time and I wanted to finish it first. So I left the code like this, I'll return to it next week or for another assignment.



Files:

Arduino libraries

Libraries add extra functions for the use of sketches. They basically contain extra definitions and anyone can create their own. They consist on two files, one with the extension ".h" - which contains definitions for the library as commands/functions and other variables. And other with ".cpp" - which has the code used to communicate and conect with the Driver. This file has complex code written in C. We normally won't read it, but we access to it through commands as "void - moveTo", "boolean - run", "float - maxSpeed", etc...

For this exercise, I wanted to use my LCD, so I needed the "LiquidCrystal.h" library which has commands like "lcd.begin", "lcd.clear", etc...

I mostly followed this tutorial, to understand the pins, where to connect them and what I could do with the code.

In brief, first I needed o connect te pins the the corresponding in the breadboard. Ground to ground, power to power, for the contrast adjust I added a potentiometer with its own legs connected to power and ground correspondingly. The data pins and backlight power and ground. After this I defined the setup and for it clear the screen and connect the pins from where I was sending the information. After the setup, I added a loop for displaying the message. Here, I had a bit of a hard time because I wanted to show the message and then move it all to the left but the screen cut the text and didn't show it all, even if it started moving as desired. In the end, I broke down the phrase into various functions and adding a delay between them to allow the screen to clear the previous information for then showing the next message.





File:

Platformio

I wanted to use the potentiometer again and do something different so I looked for an example in Arduino and chose "Bar Graph." I tried to change the code in order to use registers, but... I couldn't. So, in the end I just created the folder using Platformio (after downloading the package and adding it to Atom), imported the arduino library, understood the reason why the code was written that way and changed the parameters needed to make my own board work. Here, I had to define setup first and I used a for function, instructing it to read all from the 2 ledPin to the 9th. Then, for the loop function, I needed to read the sensor or in this case the potentiometer first and define when it was high or low so according to the number of leds it could start to light up when changing the value through the potentiometer. Finally and if funtion to make sure that if the potentiometer reading was high, it could leave the leds on and when lowering dow, it could turn them off.



File: